home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / ghspt261.gz / ghspt261 / gs261 / gstype1.c < prev    next >
C/C++ Source or Header  |  1993-05-12  |  22KB  |  704 lines

  1. /* Copyright (C) 1990, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gstype1.c */
  20. /* Adobe Type 1 font routines for Ghostscript library */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxarith.h"
  26. #include "gxfixed.h"
  27. #include "gxmatrix.h"
  28. #include "gzstate.h"
  29. #include "gzdevice.h"            /* for gxchar */
  30. #include "gxdevmem.h"            /* ditto */
  31. #include "gzpath.h"
  32. #include "gxchar.h"
  33. #include "gxfont.h"
  34. #include "gxtype1.h"
  35. #include "gxop1.h"
  36.  
  37. /* Define whether to always do Flex segments as curves. */
  38. /* This is only an issue because some old Adobe DPS fonts */
  39. /* seem to violate the Flex specification in a way that requires this. */
  40. #define ALWAYS_DO_FLEX_AS_CURVE 1
  41.  
  42. /* Encrypt a string. */
  43. int
  44. gs_type1_encrypt(byte *dest, const byte *src, uint len, crypt_state *pstate)
  45. {    register crypt_state state = *pstate;
  46.     register const byte *from = src;
  47.     register byte *to = dest;
  48.     register uint count = len;
  49.     while ( count )
  50.        {    encrypt_next(*from, state, *to);
  51.         from++, to++, count--;
  52.        }
  53.     *pstate = state;
  54.     return 0;
  55. }
  56. /* Decrypt a string. */
  57. int
  58. gs_type1_decrypt(byte *dest, const byte *src, uint len, crypt_state *pstate)
  59. {    register crypt_state state = *pstate;
  60.     register const byte *from = src;
  61.     register byte *to = dest;
  62.     register uint count = len;
  63.     while ( count )
  64.        {    /* If from == to, we can't use the obvious */
  65.         /*    decrypt_next(*from, state, *to);    */
  66.         register byte ch = *from++;
  67.         decrypt_next(ch, state, *to);
  68.         to++, count--;
  69.        }
  70.     *pstate = state;
  71.     return 0;
  72. }
  73.  
  74. /* Export the size of the structure */
  75. const uint gs_type1_state_sizeof = sizeof(gs_type1_state);
  76.  
  77. /* Imported procedures */
  78. extern int gx_matrix_to_fixed_coeff(P3(gs_matrix *, fixed_coeff *, int));
  79.  
  80. /* Initialize a Type 1 interpreter. */
  81. /* The caller must supply a string to the first call of gs_type1_interpret. */
  82. int
  83. gs_type1_init(register gs_type1_state *pis, gs_show_enum *penum,
  84.   int charpath_flag, int paint_type, gs_type1_data *pdata)
  85. {    gs_state *pgs = penum->pgs;
  86.     int log2_scale = 0;    /* change eventually */
  87.     pis->penum = penum;
  88.     pis->pgs = pgs;
  89.     pis->pdata = pdata;
  90.     pis->charpath_flag = charpath_flag;
  91.     pis->paint_type = paint_type;
  92.     pis->os_count = 0;
  93.     pis->ips_count = 1;
  94.     pis->sb_set = 0;
  95.     pis->seac_base = -1;
  96.     pis->flex_count = flex_max;        /* not in Flex */
  97.     pis->in_dotsection = 0;
  98.     pis->vstem3_set = 0;
  99.     pis->vs_offset.x = pis->vs_offset.y = 0;
  100.     set_pixel_scale(&pis->scale, log2_scale);
  101.     reset_stem_hints(pis);
  102.     gx_matrix_to_fixed_coeff(&ctm_only(pgs), &pis->fc, max_coeff_bits);
  103.     compute_font_hints(&pis->fh, &pgs->ctm, log2_scale, pdata);
  104.     /* Set the current point of the path to the origin, */
  105.     /* in anticipation of the initial [h]sbw. */
  106.     { gx_path *ppath = pgs->path;
  107.       ppath->position.x = pis->position.x = pgs->ctm.tx_fixed;
  108.       ppath->position.y = pis->position.y = pgs->ctm.ty_fixed;
  109.     }
  110.     /* Set the flatness to a value that is likely to produce */
  111.     /* reasonably good-looking curves, regardless of its */
  112.     /* current value in the graphics state. */
  113.        {    /* If the character is very small, set the flatness */
  114.         /* to zero, which will produce very accurate curves. */
  115.         float cxx = fabs(pgs->ctm.xx), cyy = fabs(pgs->ctm.yy);
  116.         if ( cyy > cxx ) cxx = cyy;
  117.         if ( is_skewed(&pgs->ctm) )
  118.            {    float cxy = fabs(pgs->ctm.xy), cyx = fabs(pgs->ctm.yx);
  119.             if ( cxy > cxx ) cxx = cxy;
  120.             if ( cyx > cxx ) cxx = cyx;
  121.            }
  122.         pis->flatness = (cxx >= 0.2 ? cxx : 0.0);
  123.        }
  124.     return 0;
  125. }
  126.  
  127. /* Tracing for type 1 interpreter */
  128. #ifdef DEBUG
  129. #  define dc(str) if ( gs_debug['1'] ) type1_trace(cip, c, str);
  130. private void near
  131. type1_trace(const byte *cip, byte c, const char _ds *str)
  132. {    dprintf3("[1]%lx: %02x %s\n", (ulong)(cip - 1), c, (char *)str);
  133. }
  134. #else
  135. #  define dc(str)
  136. #endif
  137.  
  138. /* Define the state used by operator procedures. */
  139. /* These macros refer to a current instance (s) of gs_op1_state. */
  140. #define sppath s.ppath
  141. #define sfc s.fc
  142. #define ptx s.px
  143. #define pty s.py
  144.  
  145. /* Accumulate relative coordinates */
  146. /****** THESE ARE NOT ACCURATE FOR NON-INTEGER DELTAS. ******/
  147. /* This probably doesn't make any difference in practice. */
  148. #define c_fixed(d, c) m_fixed(d, c, sfc, max_coeff_bits)
  149. #define accum_x(dx)\
  150.     ptx += c_fixed(dx, xx);\
  151.     if ( sfc.skewed ) pty += c_fixed(dx, xy)
  152. #define accum_y(dy)\
  153.     pty += c_fixed(dy, yy);\
  154.     if ( sfc.skewed ) ptx += c_fixed(dy, yx)
  155. #define accum_xy(dx,dy)\
  156.     accum_xy_proc(&s, dx, dy)
  157.  
  158. #define s (*ps)
  159.  
  160. private void near
  161. accum_xy_proc(register is_ptr ps, fixed dx, fixed dy)
  162. {    ptx += m_fixed(dx, xx, sfc, max_coeff_bits),
  163.     pty += m_fixed(dy, yy, sfc, max_coeff_bits);
  164.     if ( sfc.skewed )
  165.         ptx += m_fixed(dy, yx, sfc, max_coeff_bits),
  166.         pty += m_fixed(dx, xy, sfc, max_coeff_bits);
  167. }
  168.  
  169. /* We round all endpoints of lines or curves */
  170. /* to the nearest quarter-pixel, and suppress null lines. */
  171. /* (Rounding to the half-pixel causes too many dropouts.) */
  172. /* This saves a lot of rendering work for small characters. */
  173. #define pixel_rounded(fx)\
  174.   (((fx) + float2fixed(0.125)) & float2fixed(-0.25))
  175. #define must_draw_to(lpx, lpy, px, py)\
  176.   ((lpx = pixel_rounded(px)), (lpy = pixel_rounded(py)),\
  177.    (psub = sppath->current_subpath) == 0 ||\
  178.    (pseg = psub->last)->type == s_line_close ||\
  179.    lpx != pseg->pt.x || lpy != pseg->pt.y)
  180.  
  181. /* ------ Operator procedures ------ */
  182.  
  183. /* We put these before the interpreter to save having to write */
  184. /* prototypes for all of them. */
  185.  
  186. int
  187. gs_op1_closepath(register is_ptr ps)
  188. {    /* Note that this does NOT reset the current point! */
  189.     int code = gx_path_close_subpath(sppath);
  190.     if ( code < 0 ) return code;
  191.     return gx_path_add_point(sppath, ptx, pty);    /* put the point where it was */
  192. }
  193.  
  194. int
  195. gs_op1_sbw(register is_ptr ps, fixed sbx, fixed sby, fixed wx, fixed wy)
  196. {    register gs_type1_state *pis = ps->pis;
  197.     if ( !pis->sb_set )
  198.         pis->lsb.x = sbx, pis->lsb.y = sby;
  199.     pis->width.x = wx, pis->width.y = wy;
  200.     if_debug4('1',"[1]sb=(%g,%g) w=(%g,%g)\n",
  201.           fixed2float(pis->lsb.x), fixed2float(pis->lsb.y),
  202.           fixed2float(pis->width.x), fixed2float(pis->width.y));
  203.     accum_xy(pis->lsb.x, pis->lsb.y);
  204.     return 0;
  205. }
  206.  
  207. int
  208. gs_op1_hsbw(register is_ptr ps, fixed sbx, fixed wx)
  209. {    return gs_op1_sbw(ps, sbx, fixed_0, wx, fixed_0);
  210. }
  211.  
  212. int
  213. gs_op1_rrcurveto(register is_ptr ps, fixed dx1, fixed dy1,
  214.   fixed dx2, fixed dy2, fixed dx3, fixed dy3)
  215. {    gs_fixed_point pt1, pt2, pt3;
  216.     gs_type1_state *pis = ps->pis;
  217.     fixed ax0 = sppath->position.x - ptx;
  218.     fixed ay0 = sppath->position.y - pty;
  219.     /* Following declarations are only for must_draw_to */
  220.     fixed lpx, lpy;
  221.     subpath *psub;
  222.     segment *pseg;
  223.     accum_xy(dx1, dy1);
  224.     pt1.x = ptx + ax0, pt1.y = pty + ay0;
  225.     accum_xy(dx2, dy2);
  226.     pt2.x = ptx, pt2.y = pty;
  227.     accum_xy(dx3, dy3);
  228.     find_stem_hints(pis, ptx, pty, dx3, dy3, &pt3);
  229.     if ( must_draw_to(lpx, lpy, pt3.x, pt3.y) )
  230.     {    /* Adjust second control point for endpoint hint */
  231.         /* and rounding. */
  232.         pt2.x += lpx - ptx, pt2.y += lpy - pty;
  233.         return gx_path_add_flattened_curve(sppath, pt1.x, pt1.y,
  234.                 pt2.x, pt2.y, lpx, lpy, pis->flatness);
  235.     }
  236.     return 0;
  237. }
  238.  
  239. #undef s
  240.  
  241. /* ------ Main interpreter ------ */
  242.  
  243. /* Continue interpreting a Type 1 CharString. */
  244. /* If str != 0, it is taken as the byte string to interpret. */
  245. /* Return 0 on successful completion, <0 on error, */
  246. /* or >0 when client intervention is required. */
  247. /* The int * argument is where the character is stored for seac, */
  248. /* or the othersubr # for callothersubr. */
  249. private int near type1_endchar(P3(gs_type1_state *, gs_state *, gx_path *));
  250. int
  251. gs_type1_interpret(register gs_type1_state *pis, const byte *str,
  252.   const gs_point *psbpt, int *pindex)
  253. {    gs_state *pgs = pis->pgs;
  254.     gs_type1_data *pdata = pis->pdata;
  255.     gs_op1_state s;
  256.     fixed cstack[ostack_size];
  257. #define cs0 cstack[0]
  258. #define ics0 fixed2int_var(cs0)
  259. #define cs1 cstack[1]
  260. #define ics1 fixed2int_var(cs1)
  261. #define cs2 cstack[2]
  262. #define ics2 fixed2int_var(cs2)
  263. #define cs3 cstack[3]
  264. #define ics3 fixed2int_var(cs3)
  265. #define cs4 cstack[4]
  266. #define ics4 fixed2int_var(cs4)
  267. #define cs5 cstack[5]
  268. #define ics5 fixed2int_var(cs5)
  269.     register fixed _ss *csp;
  270. #define clear csp = cstack - 1
  271.     ip_state *ipsp = &pis->ipstack[pis->ips_count - 1];
  272.     register const byte *cip;
  273.     register crypt_state state;
  274.     register int c;
  275.     int code = 0;
  276.     fixed ftx = pgs->ctm.tx_fixed, fty = pgs->ctm.ty_fixed;
  277.     gs_fixed_point hpt;
  278.     /* Following are only for must_draw_to */
  279.     fixed lpx, lpy;
  280.     subpath *psub;
  281.     segment *pseg;
  282.  
  283.     sppath = pgs->path;
  284.     s.pis = pis;
  285.     sfc = pis->fc;
  286.     ptx = pis->position.x;
  287.     pty = pis->position.y;
  288.  
  289.     /* Check for a side bearing override. */
  290.     if ( psbpt != 0 )
  291.     {    gs_point_transform2fixed(&pgs->ctm, psbpt->x, psbpt->y,
  292.                      &pis->lsb);
  293.         pis->sb_set = 1;
  294.     }
  295.  
  296.     /* Copy the operand stack out of the saved state. */
  297.     if ( pis->os_count == 0 )
  298.        {    clear;
  299.        }
  300.     else
  301.        {    memcpy(cstack, pis->ostack, pis->os_count * sizeof(fixed));
  302.         csp = &cstack[pis->os_count - 1];
  303.        }
  304.  
  305.     if ( str == 0 ) goto cont;
  306.     cip = str;
  307. call:    state = crypt_charstring_seed;
  308.        {    int skip = pdata->lenIV;
  309.         /* Skip initial random bytes */
  310.         for ( ; skip > 0; --skip )
  311.            {    decrypt_skip_next(*cip, state); ++cip;
  312.            }
  313.        }
  314.     goto top;
  315. cont:    cip = ipsp->ip;
  316.     state = ipsp->dstate;
  317. top:    while ( 1 )
  318.      { uint c0;
  319.        c = decrypt_this((c0 = *cip++), state);
  320.        decrypt_skip_next(c0, state);
  321.        switch ( (char_command)c )
  322.        {
  323. #define cnext clear; goto top
  324. #define inext goto top
  325.     case c_hstem: dc("hstem")
  326.         type1_hstem(pis, cs0, cs1);
  327.         cnext;
  328.     case c_vstem: dc("vstem")
  329.         type1_vstem(pis, cs0, cs1);
  330.         cnext;
  331.     case c_vmoveto: dc("vmoveto")
  332.         cs1 = cs0;
  333.         cs0 = 0;
  334.         accum_y(cs1);
  335. move:        /* cs0 = dx, cs1 = dy for hint checking. */
  336.         find_stem_hints(pis, ptx, pty, cs0, cs1, &hpt);
  337.         /* Round to the nearest center of a quarter-pixel. */
  338.         if ( must_draw_to(lpx, lpy, hpt.x, hpt.y) )
  339.             code = gx_path_add_point(sppath, lpx, lpy);
  340.         goto cc;
  341.     case c_rlineto: dc("rlineto")
  342.         accum_xy(cs0, cs1);
  343. line:        /* cs0 = dx, cs1 = dy for hint checking. */
  344.         find_stem_hints(pis, ptx, pty, cs0, cs1, &hpt);
  345.         /* Round to the nearest center of a quarter-pixel. */
  346.         if ( must_draw_to(lpx, lpy, hpt.x, hpt.y) )
  347.             code = gx_path_add_line(sppath, lpx, lpy);
  348. cc:        if ( code < 0 ) return code;
  349. pp:        if_debug2('1', "[1]pt=(%g,%g)\n",
  350.               fixed2float(ptx), fixed2float(pty));
  351.         cnext;
  352.     case c_hlineto: dc("hlineto")
  353.         accum_x(cs0);
  354.         cs1 = 0;
  355.         goto line;
  356.     case c_vlineto: dc("vlineto")
  357.         cs1 = cs0;
  358.         cs0 = 0;
  359.         accum_y(cs1);
  360.         goto line;
  361.     case c_rrcurveto: dc("rrcurveto")
  362.         code = gs_op1_rrcurveto(&s, cs0, cs1, cs2, cs3, cs4, cs5);
  363.         goto cc;
  364.     case c_closepath: dc("closepath")
  365.         code = gs_op1_closepath(&s);
  366.         goto cc;
  367.     case c_callsubr: dc("callsubr")
  368.        {    int index = fixed2int_var(*csp);
  369.         const byte *nip;
  370.         code = (*pdata->subr_proc)(pdata, index, &nip);
  371.         if ( code < 0 ) return_error(code);
  372.         --csp;
  373.         ipsp->ip = cip, ipsp->dstate = state;
  374.         ++ipsp;
  375.         cip = nip;
  376.        }
  377.         goto call;
  378.     case c_return: dc("return")
  379.         --ipsp;
  380.         goto cont;
  381.     case c_escape: dc("escape:")
  382.         decrypt_next(*cip, state, c); ++cip;
  383.         switch ( (char_extended_command)c )
  384.            {
  385.         case ce_dotsection: dc("  dotsection")
  386.             pis->in_dotsection = !pis->in_dotsection;
  387.             cnext;
  388.         case ce_vstem3: dc("  vstem3")
  389.             if ( !pis->vstem3_set && pis->fh.use_x_hints )
  390.             {    center_vstem(pis, pis->lsb.x + cs2, cs3);
  391.                 /* Adjust the current point */
  392.                 /* (center_vstem handles everything else). */
  393.                 ptx += pis->vs_offset.x;
  394.                 pty += pis->vs_offset.y;
  395.                 pis->vstem3_set = 1;
  396.             }
  397.             type1_vstem(pis, cs0, cs1);
  398.             type1_vstem(pis, cs2, cs3);
  399.             type1_vstem(pis, cs4, cs5);
  400.             cnext;
  401.         case ce_hstem3: dc("  hstem3")
  402.             type1_hstem(pis, cs0, cs1);
  403.             type1_hstem(pis, cs2, cs3);
  404.             type1_hstem(pis, cs4, cs5);
  405.             cnext;
  406.         case ce_seac: dc("  seac")
  407.             /* Do the accent now.  When it finishes */
  408.             /* (detected in endchar), do the base character. */
  409.             pis->seac_base = ics3;
  410.             /* Adjust the origin of the coordinate system */
  411.             /* for the accent (endchar puts it back). */
  412.             ptx = ftx, pty = fty;
  413.             /* The Adobe documentation says that adx/ady are */
  414.             /* the offset for the *origin*, but they're */
  415.             /* actually the offset for the *lsb*: */
  416.             cs1 += pis->lsb.x - cs0;
  417.             cs2 += pis->lsb.y;
  418.             accum_xy(cs1, cs2);
  419.             sppath->position.x = pis->position.x = ptx;
  420.             sppath->position.y = pis->position.y = pty;
  421.             pis->os_count = 0;    /* clear */
  422.             /* Give control back to the caller, who must */
  423.             /* re-invoke the interpreter with the seac string. */
  424.             *pindex = ics4;
  425.             return type1_result_seac;
  426.         case ce_sbw: dc("  sbw")
  427.             code = gs_op1_sbw(&s, cs0, cs1, cs2, cs3);
  428.             goto cc;
  429.         case ce_div: dc("  div")
  430.             csp[-1] = float2fixed((float)csp[-1] / (float)*csp);
  431.             --csp; goto pushed;
  432.         case ce_undoc15: dc("  undoc15")
  433.             /* See gstype1.h for information on this opcode. */
  434.             cnext;
  435.         case ce_callothersubr: dc("  callothersubr")
  436. #define fpts pis->flex_points
  437. /* Check that the next opcode is a `pop'. */
  438. #define check_next_pop()\
  439.   decrypt_next(*cip, state, c);\
  440.   if ( c != c_escape ) return_error(gs_error_invalidfont);\
  441.   decrypt_next(cip[1], state, c);\
  442.   if ( c != ce_pop ) return_error(gs_error_invalidfont);\
  443.   cip += 2
  444.             /* We must remember to pop both the othersubr # */
  445.             /* and the argument count off the stack. */
  446.             switch ( *pindex = fixed2int_var(*csp) )
  447.             {
  448.             case 0:
  449.             {    /* We have to do something really sleazy */
  450.                 /* here, namely, make it look as though */
  451.                 /* the rmovetos never really happened, */
  452.                 /* because we don't want to interrupt */
  453.                 /* the current subpath. */
  454.                 gs_fixed_point ept;
  455.                 fixed fheight = csp[-4];
  456.                 gs_fixed_point hpt;
  457.                 if ( pis->flex_count != 8 )
  458.                     return_error(gs_error_invalidfont);
  459.                 /* Make sure the next two opcodes */
  460.                 /* are `pop' `pop'. */
  461.                 check_next_pop();
  462.                 check_next_pop();
  463.                 csp[-4] = csp[-3];
  464.                 csp[-3] = csp[-2];
  465.                 csp -= 3;
  466.                 gx_path_current_point(sppath, &ept);
  467.                 gx_path_add_point(sppath, fpts[0].x, fpts[0].y);
  468.                 sppath->subpath_open =    /* <--- sleaze */
  469.                     pis->flex_path_was_open;
  470.                 /* Decide whether to do the flex as a curve. */
  471.                 hpt.x = fpts[1].x - fpts[4].x;
  472.                 hpt.y = fpts[1].y - fpts[4].y;
  473.                 if_debug3('1',
  474.                       "[1]flex: d=(%g,%g), height=%g\n",
  475.                       fixed2float(hpt.x), fixed2float(hpt.y),
  476.                       fixed2float(fheight) / 100);
  477. #if !ALWAYS_DO_FLEX_AS_CURVE            /* See beginning of file. */
  478.                 if ( any_abs(hpt.x) + any_abs(hpt.y) <
  479.                      fheight / 100
  480.                    )
  481.                 {    /* Do the flex as a line. */
  482.                     code = gx_path_add_line(sppath,
  483.                                 ept.x, ept.y);
  484.                 }
  485.                 else
  486. #endif
  487.                 {    /* Do the flex as a curve. */
  488.                     code = gx_path_add_flattened_curve(sppath,
  489.                         fpts[2].x, fpts[2].y,
  490.                         fpts[3].x, fpts[3].y,
  491.                         fpts[4].x, fpts[4].y,
  492.                         pis->flatness);
  493.                     if ( code < 0 ) return code;
  494.                     code = gx_path_add_flattened_curve(sppath,
  495.                         fpts[5].x, fpts[5].y,
  496.                         fpts[6].x, fpts[6].y,
  497.                         fpts[7].x, fpts[7].y,
  498.                         pis->flatness);
  499.                 }
  500.             }
  501.                 if ( code < 0 ) return code;
  502.                 pis->flex_count = flex_max;    /* not inside flex */
  503.                 inext;
  504.             case 1:
  505.                 gx_path_current_point(sppath, &fpts[0]);
  506.                 pis->flex_path_was_open = /* <--- more sleaze */
  507.                     sppath->subpath_open;
  508.                 pis->flex_count = 1;
  509.                 csp -= 2;
  510.                 inext;
  511.             case 2:
  512.                 if ( pis->flex_count >= flex_max )
  513.                     return_error(gs_error_invalidfont);
  514.                 gx_path_current_point(sppath,
  515.                         &fpts[pis->flex_count++]);
  516.                 csp -= 2;
  517.                 inext;
  518.             case 3:
  519.                 /* Make sure the next opcode is a `pop'. */
  520.                 check_next_pop();
  521.                 reset_stem_hints(pis);
  522.                 csp -= 2;
  523.                 inext;
  524.             }
  525. #undef fpts
  526.             /* Not a recognized othersubr, */
  527.             /* let the client handle it. */
  528.            {    int scount = csp - cstack;
  529.             pis->position.x = ptx;
  530.             pis->position.y = pty;
  531.             /* Exit to caller */
  532.             ipsp->ip = cip, ipsp->dstate = state;
  533.             pis->os_count = scount;
  534.             pis->ips_count = ipsp - &pis->ipstack[0] + 1;
  535.             if ( scount )
  536.                 memcpy(pis->ostack, cstack,
  537.                        scount * sizeof(fixed));
  538.             return type1_result_callothersubr;
  539.            }
  540.         case ce_pop: dc("  pop")
  541.             ++csp;
  542.             code = (*pdata->pop_proc)(pdata, csp);
  543.             if ( code < 0 ) return_error(code);
  544.             goto pushed;
  545.         case ce_setcurrentpoint: dc("  setcurrentpoint")
  546.             ptx = ftx, pty = fty;
  547.             accum_xy(cs0, cs1);
  548.             goto pp;
  549.         default:
  550.             return_error(gs_error_invalidfont);
  551.            }
  552.         break;
  553.     case c_hsbw: dc("hsbw")
  554.         code = gs_op1_hsbw(&s, cs0, cs1);
  555.         goto cc;
  556.     case c_endchar: dc("endchar")
  557.         if ( pis->seac_base >= 0 )
  558.            {    /* We just finished the accent of a seac. */
  559.             /* Do the base character. */
  560.             *pindex = pis->seac_base;
  561.             pis->seac_base = -1;
  562.             /* Restore the coordinate system origin */
  563.             sppath->position.x = pis->position.x = ftx;
  564.             sppath->position.y = pis->position.y = fty;
  565.             pis->os_count = 0;    /* clear */
  566.             /* Clear the ipstack, in case the accent ended */
  567.             /* inside a subroutine. */
  568.             pis->ips_count = 1;
  569.             /* Give control back to the caller, who must */
  570.             /* re-invoke the interpreter with the */
  571.             /* base character string. */
  572.             return type1_result_seac;
  573.            }
  574.         /* This is a real endchar.  Handle it below. */
  575.         return type1_endchar(pis, pgs, sppath);
  576.     case c_undoc15: dc("  undoc15")
  577.         /* See gstype1.h for information on this opcode. */
  578.         cnext;
  579.     case c_rmoveto: dc("rmoveto")
  580.         accum_xy(cs0, cs1);
  581.         goto move;
  582.     case c_hmoveto: dc("hmoveto")
  583.         accum_x(cs0);
  584.         cs1 = 0;
  585.         goto move;
  586.     case c_vhcurveto: dc("vhcurveto")
  587.         code = gs_op1_rrcurveto(&s, fixed_0, cs0, cs1, cs2, cs3, fixed_0);
  588.         goto cc;
  589.     case c_hvcurveto: dc("hvcurveto")
  590.         code = gs_op1_rrcurveto(&s, cs0, fixed_0, cs1, cs2, fixed_0, cs3);
  591.         goto cc;
  592.     /* Fill up the dispatch up to 32. */
  593.     case c_undef0: case c_undef2:
  594.     case c_undef16: case c_undef17: case c_undef18: case c_undef19:
  595.     case c_undef20: case c_undef23:
  596.     case c_undef24: case c_undef25: case c_undef26: case c_undef27:
  597.     case c_undef28: case c_undef29:
  598.         return_error(gs_error_invalidfont);
  599.     /* Fill up the dispatch for 1-byte numbers. */
  600. #define icase(n) case n:
  601. #define ncase(n) case n: *++csp = int2fixed(c_value_num1(n)); goto pushed;
  602. #define icase10(n)\
  603.   icase(n) icase(n+1) icase(n+2) icase(n+3) icase(n+4)\
  604.   icase(n+5) icase(n+6) icase(n+7) icase(n+8) icase(n+9)
  605. #define ncase10(n)\
  606.   ncase(n) ncase(n+1) ncase(n+2) ncase(n+3) ncase(n+4)\
  607.   ncase(n+5) ncase(n+6) ncase(n+7) ncase(n+8) ncase(n+9)
  608.     icase(32) icase(33) icase(34)
  609.     icase(35) icase(36) icase(37) icase(38) icase(39)
  610.     icase10(40)
  611.     icase10(50) icase10(60) icase10(70) icase10(80) icase10(90)
  612.     icase10(100) icase10(110) goto pi; ncase10(120) ncase10(130) ncase10(140)
  613.     ncase10(150) icase10(160) icase10(170) icase10(180) icase10(190)
  614.     icase10(200) icase10(210) icase10(220) icase10(230)
  615.     icase(240) icase(241) icase(242) icase(243) icase(244)
  616.     icase(245) icase(246)
  617. pi:        *++csp = int2fixed(c_value_num1(c));
  618. pushed:        if_debug3('1', "[1]%d: (%d) %f\n",
  619.               (int)(csp - cstack), c, fixed2float(*csp));
  620.         break;
  621.     /* Handle 2-byte positive numbers. */
  622. #define case_num2(n)\
  623.   case c_num2+n: c = c_value_num2(c_num2+n, 0)
  624.     case_num2(0); goto pos2;
  625.     case_num2(1); goto pos2;
  626.     case_num2(2); goto pos2;
  627.     case_num2(3);
  628. #undef case_num2
  629. pos2:       {    c0 = *cip++;
  630.         if_debug2('1', "[1] (%d)+%d\n",
  631.               c, decrypt_this(c0, state));
  632.         *++csp = int2fixed((int)decrypt_this(c0, state) + c);
  633.         decrypt_skip_next(c0, state);
  634.        }    goto pushed;
  635.     /* Handle 2-byte negative numbers. */
  636. #define case_num3(n)\
  637.   case c_num3+n: c = c_value_num3(c_num3+n, 0)
  638.     case_num3(0); goto neg2;
  639.     case_num3(1); goto neg2;
  640.     case_num3(2); goto neg2;
  641.     case_num3(3);
  642. #undef case_num3
  643. neg2:       {    c0 = *cip++;
  644.         if_debug2('1', "[1] (%d)-%d\n",
  645.               c, decrypt_this(c0, state));
  646.         *++csp = int2fixed(c - (int)decrypt_this(c0, state));
  647.         decrypt_skip_next(c0, state);
  648.        }    goto pushed;
  649.     /* Handle 5-byte numbers. */
  650.     case c_num4:
  651.        {    long lw = 0;
  652.         int i;
  653.         for ( i = 4; --i >= 0; )
  654.         {    decrypt_next(*cip, state, c0);
  655.             lw = (lw << 8) + c0;
  656.             cip++;
  657.         }
  658.         *++csp = int2fixed(lw);
  659.         if ( lw != fixed2long(*csp) )
  660.             return_error(gs_error_rangecheck);
  661.        }    goto pushed;
  662.        }
  663.      }
  664. }
  665.  
  666. /* Pop a (fixed) number off the internal stack. */
  667. /* The client uses this to get the arguments for an OtherSubr. */
  668. int
  669. gs_type1_pop(gs_type1_state *pis, fixed *pf)
  670. {    *pf = pis->ostack[--(pis->os_count)];
  671.     return 0;
  672. }
  673.  
  674. /* ------ Termination ------ */
  675.  
  676. /* Handle the end of a character. */
  677. private int near
  678. type1_endchar(gs_type1_state *pis, gs_state *pgs, gx_path *ppath)
  679. {    /* Set the current point to the character origin */
  680.     /* plus the width. */
  681.     gs_moveto(pgs, fixed2float(pis->width.x), fixed2float(pis->width.y));
  682.     /* If there are no hints, ... */
  683.     if ( pis->fh.snap_h.count || pis->fh.snap_v.count ||
  684.          pis->fh.a_zone_count
  685.        )
  686.     {    pgs->fill_adjust = fixed_0;
  687.         return 0;
  688.     }
  689.     /* ... tweak the fill adjustment. */
  690.     /* This is based on experience, not theory! */
  691.     {    gs_fixed_rect bbox;
  692.         int dx, dy, dmax;
  693.         gx_path_bbox(pgs->path, &bbox);
  694.         dx = fixed2int_ceiling(bbox.q.x - bbox.p.x);
  695.         dy = fixed2int_ceiling(bbox.q.y - bbox.p.y);
  696.         dmax = max(dx, dy);
  697.         pgs->fill_adjust =
  698.             (dmax < 10 ? float2fixed(0.2) :
  699.              dmax < 25 ? float2fixed(0.1) :
  700.              float2fixed(0.05));
  701.     }
  702.     return 0;
  703. }
  704.